//
//  KYDiseaseClassView.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/18.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYDiseaseClassView.h"
#import "KYLittleClassView.h"
#import "Header.h"
#import <Masonry.h>
#import "KYButton.h"

@interface KYDiseaseClassView ()
@property (nonatomic, strong) NSArray *modelList;
@property (nonatomic, strong)UIView *contentView;
@property (nonatomic, assign)NSInteger selectView;
@property (nonatomic, strong)KYDiseaseClassModel *selectModel;
@property (nonatomic, assign)NSInteger selectCount;
@end
@implementation KYDiseaseClassView

-(NSArray *)modelList{
    if (!_modelList) {
        _modelList = [KYDiseaseClassModel getDiseaseClassModel];
    }
    return _modelList;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpUI];
    }
    return self;
}
-(void)setUpUI{
    UILabel *textLabel = [[UILabel alloc]init];
    textLabel.text = @"请选择疾病类型";
    textLabel.textColor = [UIColor blueColor];
    textLabel.font = [UIFont systemFontOfSize:20];
    textLabel.frame = CGRectMake(10, 15, ScreenWith, 30);
    [self addSubview:textLabel];
    for (int i = 0; i<self.modelList.count; i++) {
        KYLittleClassView *view = [[KYLittleClassView alloc]init];
        view.layer.cornerRadius = 10;
        view.layer.masksToBounds = YES;
        view.tag = i;
        int rowCount = 2;
        int col=i%rowCount;
        CGFloat appW = (ScreenWith - 30 -10)/rowCount;
        CGFloat x = 15 + (appW +10) *col;
        CGFloat appH = 200;
        int row = i/rowCount;
        CGFloat y = 70 + (appH + 10) *row;
        view.frame = CGRectMake(x, y, appW, appH);
        view.diseaseClassModel = self.modelList[i];
        UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewKlick:)];
        [view addGestureRecognizer:gesture];
        [self addSubview:view];
    }
}
-(void)viewKlick:(UIGestureRecognizer *)gestureRecognizer{
    KYLittleClassView *view = (KYLittleClassView *)gestureRecognizer.view;
    self.selectView = view.tag;
//    NSLog(@"%zd",view.tag);
    self.selectModel = self.modelList[self.selectView];
    UIView *coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0.3;
    [self addSubview:coverView];
    [self addContentView];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(coverView.mas_centerX);
        make.top.equalTo(coverView.mas_top).offset(200);
        make.width.equalTo(@250);
        make.height.equalTo(@300);
    }];
    
    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverViewKlick:)];
    [coverView addGestureRecognizer:tap];
}
-(void)coverViewKlick:(UIGestureRecognizer *)gestureRecognizer{
    UIView *coverView = gestureRecognizer.view;
    [self.contentView removeFromSuperview];
    [coverView removeFromSuperview];
}
-(void)addContentView{
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    self.contentView = contentView;
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg"]];
    image.frame = CGRectMake(0, 0, 250, 44);
    [self.contentView addSubview:image];
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"请选择具体类型";
    titleLabel.textColor = [UIColor blueColor];
    titleLabel.frame = CGRectMake(10, 0, 250, 44);
    [self.contentView addSubview:titleLabel];
    KYDiseaseClassModel *model = self.modelList[self.selectView];
    
    for (int i = 0; i< model.classArray.count; i++) {
        KYButton *button = [[KYButton alloc]init];
        button.tag = i;
        [button setTitle:model.classArray[i] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, 50+(40*i), 250, 40);
        [button addTarget:self action:@selector(classBtnKlick:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:button];
    }
}
-(void)classBtnKlick:(UIButton *)button{
    self.selectCount = button.tag;
    button.selected = button.selected ? NO:YES;
    [[NSNotificationCenter defaultCenter]postNotificationName:changeViewNotification object:nil userInfo:@{changeViewKey:@"classBtnKlick",@"selectModel":self.selectModel,@"selectCount":@(self.selectCount)}];
}
@end
